﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace NCKH.Personal
{
    public partial class NhomthuchienDT : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hienDonvi();
                hienDonviTV();
                HienDSTK();
            }
        }
        protected void TVNhomNNC_Insert(string Vaitro, int Manhom, string MaNNC)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("TVnhom_NNC", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@Vaitro", Vaitro);
                    Cmd.Parameters.AddWithValue("@Manhom", Manhom);
                    Cmd.Parameters.AddWithValue("@MaNNC", MaNNC);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected void TVNhomSV_Insert(string Vaitro,int Manhom, string MaSV)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("TVnhom_SV", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@Vaitro", Vaitro);
                    Cmd.Parameters.AddWithValue("@Manhom", Manhom);
                    Cmd.Parameters.AddWithValue("@MaSV", MaSV);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected int layManhom()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlManhom = "Select Manhom From NHOMTHUCHIEN Where Tennhom like N'"+ txtTennhom.Text+"'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlManhom, Cnn))
                {
                    int Manhom = 0;
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    Manhom = (int)Cmd.ExecuteScalar();
                    return Manhom;
                }
            }
        }
        protected string LayMaNNC(int TaikhoanID)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("MaNNC_Select", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaTK", TaikhoanID);
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        string NNCID = String.Empty;
                        if (tbl.Rows.Count > 0)
                        {
                            DataRow dr = tbl.Rows[0];
                            object mannc = dr["MaNNC"];
                            NNCID = (string)mannc;
                        }
                        return NNCID;
                    }
                }
            }
        }
        protected string LayMaSV(int TaikhoanID)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("MaSV_Select", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaTK", TaikhoanID);
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        string SvienID = String.Empty;
                        if (tbl.Rows.Count > 0)
                        {
                            DataRow dr = tbl.Rows[0];
                            object masv = dr["MaSV"];
                            SvienID = (string)masv;
                            
                        }
                        return SvienID;
                    }
                }
            }
        }
       /* protected void hienVaitro()
        { 
            ddlDonviTV.Items.Add(new ListItem(
        }*/
        
        protected void HienDSTK()
        {
            
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqllayTK = "Select MaTK,Tendangnhap,Email From TAIKHOAN Where MaQ=3";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            { 
                using (SqlCommand Cmd = new SqlCommand(sqllayTK,Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        grvHienDS.DataSource = tbl;
                        grvHienDS.DataBind();
                    }
                }
            }
        }
        protected void NhomTH_Insert()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Nhomthuchien_Insert", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@Tennhom", txtTennhom.Text);
                    Cmd.Parameters.AddWithValue("@MaDV", ddlDonvi.SelectedValue);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected void hienDonviTV()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlDonvi_Select = "Select MaDV, TenDV From DONVI";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlDonvi_Select, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        while (rd.HasRows && rd.Read())
                        {
                            ddlDonviTV.Items.Add(
                                new ListItem(rd["TenDV"].ToString(), rd["MaDV"].ToString()));
                        }
                        rd.Close();

                    }
                    Cnn.Close();
                }
            }
        }
        protected void hienDonvi()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlDonvi_Select = "Select MaDV, TenDV From DONVI";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlDonvi_Select, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        while (rd.HasRows && rd.Read())
                        {
                            ddlDonvi.Items.Add(
                                new ListItem (rd["TenDV"].ToString(),rd["MaDV"].ToString()));
                        }
                        rd.Close();

                    }
                    Cnn.Close();
                }
            }
        }
       
        protected void btnLapnhom_Click(object sender, EventArgs e)
        {
            NhomTH_Insert();
            plThanhvien.Visible = true;
        }

        protected void grvHienDS_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("them"))
            {
                //string Vaitro = ((DropDownList)row.FindControl("ddlVaitro")).SelectedItem.Text;
                    int RowIndex = int.Parse(e.CommandArgument.ToString().Trim());

                    DropDownList ddlVaitro = (DropDownList)grvHienDS.Rows[RowIndex].FindControl("ddlVaitro");

                    string Vaitro = ddlVaitro.SelectedItem.Text;
                    /*if (Vaitro != null)
                    {
                        lblthongbao.Text = "Item Found: " + Vaitro;
                    }
                    else
                    {
                        lblthongbao.Text = "Item not Found: " + Vaitro;
                    }*/
                int TaikhoanID = Convert.ToInt32(e.CommandArgument);
                string MaSV = LayMaSV(TaikhoanID);
                string MaNNC = LayMaNNC(TaikhoanID);
                int Manhom = layManhom();
                //lblthongbao.Text = "Mã: " + MaNNC + MaSV;
                
                if (MaSV!=String.Empty)
                {   
                   TVNhomSV_Insert(Vaitro, Manhom, MaSV);
                    lblthongbao.Text = "Mã sinh viên" + MaSV;
                    
                }
                if (MaNNC!=String.Empty)
                {
                    TVNhomNNC_Insert(Vaitro, Manhom, MaNNC);
                    lblthongbao.Text = "Mã NNC" + MaNNC;
                }
            }
        }
        public int count;
        protected void grvHienDS_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //DataRowView oRow = (DataRowView)e.Row.DataItem;

                ImageButton lnkPostType = (ImageButton)e.Row.FindControl("lbntThem");

                lnkPostType.CommandArgument = count.ToString();

                count++;

            }
        }
    }
}